//
//  haViewController.m
//  XYPieChart
//
//  Created by Mac on 15/10/7.
//  Copyright © 2015年 Xiaoyang Feng. All rights reserved.
//

#import "XYViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "AppDelegate.h"

@interface XYViewController ()

@end

@implementation XYViewController
@synthesize pieChartRight = _pieChart;
@synthesize percentageLabel = _percentageLabel;
@synthesize selectedSliceLabel = _selectedSlice;
@synthesize numOfSlices = _numOfSlices;
@synthesize indexOfSlices = _indexOfSlices;
@synthesize downArrow = _downArrow;
@synthesize slices = _slices;
@synthesize sliceColors = _sliceColors;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.slices = [NSMutableArray arrayWithCapacity:10];
    if (DeviceType >= 4 && DeviceType < 5) {
      _pieChart = [[XYPieChart alloc] initWithFrame:CGRectMake1(0, 0, 115, 115)];
    }else if (DeviceType >= 5 && DeviceType < 6){
     _pieChart = [[XYPieChart alloc] initWithFrame:CGRectMake1(0, 0, 145, 145)];
    }else{
     _pieChart = [[XYPieChart alloc] initWithFrame:CGRectMake1(0, 0, 160, 160)];
    }
    [self.view addSubview:_pieChart];
  
    self.slices = [NSMutableArray arrayWithCapacity:10];
    
    //    for(int i = 0; i < 5; i ++)
    //    {
    //        NSNumber *one = [NSNumber numberWithInt:rand()%60+20];
    //        [_slices addObject:one];
    //    }
    [_slices addObject:self.smalll];
    [_slices addObject:self.sum];
    [self.pieChartRight setDelegate:self];
    [self.pieChartRight setDataSource:self];
    if (DeviceType >= 4 && DeviceType < 5) {
       [self.pieChartRight setPieCenter:CGPointMake(76, 80)];
    }else if (DeviceType >= 5 && DeviceType < 6){
       [self.pieChartRight setPieCenter:CGPointMake(85, 96)];
    }else{
       [self.pieChartRight setPieCenter:CGPointMake(100, 130)];
    }
    
    [self.pieChartRight setShowPercentage:NO];
    
    //    [self.percentageLabel.layer setCornerRadius:90];
    
    self.sliceColors =[NSArray arrayWithObjects:
                       [UIColor colorWithRed:188/255.0 green:188/255.0  blue:188/255.0  alpha:1],
                       [UIColor greenColor],
                        nil];
}

- (void)viewDidUnload
{
    [self setPieChartRight:nil];
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    //    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.pieChartRight reloadData];
}

- (void)viewWillDisappear:(BOOL)animated
{
    //	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    //	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}


#pragma mark - XYPieChart Data Source

- (NSUInteger)numberOfSlicesInPieChart:(XYPieChart *)pieChart
{
    return self.slices.count;
}

- (CGFloat)pieChart:(XYPieChart *)pieChart valueForSliceAtIndex:(NSUInteger)index
{
    return [[self.slices objectAtIndex:index] intValue];
}

- (UIColor *)pieChart:(XYPieChart *)pieChart colorForSliceAtIndex:(NSUInteger)index
{
    if(pieChart == self.pieChartRight) return [self.sliceColors objectAtIndex:(index % self.sliceColors.count)];
    return [self.sliceColors objectAtIndex:(index % self.sliceColors.count)];
}

#pragma mark - XYPieChart Delegate
- (void)pieChart:(XYPieChart *)pieChart didSelectSliceAtIndex:(NSUInteger)index
{
    //    NSLog(@"did select slice at index %d",index);
    //    self.selectedSliceLabel.text = [NSString stringWithFormat:@"$%@",[self.slices objectAtIndex:index]];
}

CG_INLINE CGRect
CGRectMake1(CGFloat x, CGFloat y, CGFloat width, CGFloat height)

{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    CGRect rect;
    rect.origin.x = x * myDelegate.autoSizeScaleX; rect.origin.y = y * myDelegate.autoSizeScaleY;
    rect.size.width = width * myDelegate.autoSizeScaleX; rect.size.height = height * myDelegate.autoSizeScaleY;
    return rect;
}
@end
